package com.github.codemperor.core.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class ComputeUtil {

    /**
     * list 数组 排列组合
     * @param dataList
     * @param index
     * @param resultList
     * @return
     */
    public static List<Object[]> combination(List<Object[]> dataList, int index, List<Object[]> resultList) {
        if (index == dataList.size()) {
            return resultList;
        }

        List<Object[]> resultList0 = new ArrayList<>();
        if (index == 0) {
            Object[] objArr = dataList.get(0);
            for (Object obj : objArr) {
                resultList0.add(new Object[]{obj});
            }
        } else {
            Object[] objArr = dataList.get(index);
            for (Object[] objArr0 : resultList) {
                for (Object obj : objArr) {
                    //复制数组并扩充新元素
                    Object[] objArrCopy = new Object[objArr0.length + 1];
                    System.arraycopy(objArr0, 0, objArrCopy, 0, objArr0.length);
                    objArrCopy[objArrCopy.length - 1] = obj;
                    //追加到结果集
                    resultList0.add(objArrCopy);
                }
            }
        }
        return combination(dataList, ++index, resultList0);
    }

    /**
     * list 排列组合
     * @param dataList
     * @param index
     * @param resultList
     * @return
     */
    public static List<List<Object>> listCombination(List<List<Object>> dataList, int index, List<List<Object>> resultList) {
        if (index == dataList.size()) {
            return resultList;
        }
        List<List<Object>> resultList0 = new ArrayList<>();
        if (index == 0) {
            List<Object> objArr = dataList.get(0);
            for (Object obj : objArr) {
                resultList0.add(Collections.singletonList(obj));

            }
        } else {
            List<Object> objArr = dataList.get(index);
            for (List<Object> objArr0 : resultList) {
                for (Object obj : objArr) {
                    List<Object> objArrCopy = new ArrayList<>(objArr0);
                    objArrCopy.add(obj);
                    resultList0.add(objArrCopy);
                }
            }
        }
        return listCombination(dataList, ++index, resultList0);
    }

    public static void main(String[] args) {
        Object[] arr1 = new Object[]{"A1", "A2", "A3"};
        Object[] arr2 = new Object[]{"B1", "B2", "B3", "B4"};
        Object[] arr3 = new Object[]{"C1", "C2", "C3", "C4", "C5"};
        Object[] arr4 = new Object[]{"D1", "D2"};

        List<Object[]> dataList = new ArrayList<Object[]>();
        dataList.add(arr1);
        dataList.add(arr2);
        dataList.add(arr3);
        dataList.add(arr4);
        List<Object[]> resultList = combination(dataList, 0, null);

        //打印组合结果 
        for (int i = 0; i < resultList.size(); i++) {
            Object[] objArr = resultList.get(i);
            System.out.print("\n组合" + (i + 1) + "---");
            for (Object obj : objArr) {
                System.out.print(obj + " ");
            }
        }

        List<Object> list1 = Arrays.asList(1L, 2L, 3L);
        List<Object> list2 = Arrays.asList("B1", "B2", "B3", "B4");
        List<Object> list3 = Arrays.asList("C1", "C2", "C3", "C4", "C5");
        List<Object> list4 = Arrays.asList("D1", "D2");

        List<List<Object>> dataArrayList = new ArrayList<>();
        //TODO 这里千万别用Collections.singletonList(),会合并所有list
        dataArrayList.add(list1);
        dataArrayList.add(list2);
        dataArrayList.add(list3);
        dataArrayList.add(list4);
        List<List<Object>> resultListComb = listCombination(dataArrayList, 0, null);

        //打印组合结果 
        for (int i = 0; i < resultListComb.size(); i++) {
            List<Object> objArr = resultListComb.get(i);
            System.out.print("\n全新组合" + (i + 1) + "---");
            for (Object obj : objArr) {
                System.out.print(obj + " ");
            }
        }
    }
}
